package com.example.user.customviewdemo.removeitemlistview;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.example.user.customviewdemo.R;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

public class RemoveItemListViewActivity extends AppCompatActivity {

    @Bind(R.id.remove_item_listview)
    RemoveItemListView removeItemListview;

    private View preView;
    private ArrayList<String> data = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_remove_item_list_view);
        ButterKnife.bind(this);

        RemoveItemListViewAdapter adapter = new RemoveItemListViewAdapter(this);
        removeItemListview.setAdapter(adapter);
        data = getData();
        adapter.setData(data);

        removeItemListview.setDeleteButtonID(R.id.item_remove_item_listview_delete_textview);
        removeItemListview.setOnItemScrollListener(new RemoveItemListView.OnItemScrollListener() {
            @Override
            public void onItemScroll(int position,View view) {
                if(preView != null && preView != view){
                    removeItemListview.smoothScrollItemBack(position,preView);
                }
                preView = view;
            }
        });
        removeItemListview.setOnItemDeleteListener(new RemoveItemListView.OnItemDeleteListener() {
            @Override
            public void OnItemDelete(int position, ListAdapter adapter) {
                data.remove(position);
                ((BaseAdapter)adapter).notifyDataSetChanged();
            }
        });

    }

    private ArrayList<String> getData(){
        ArrayList<String> data = new ArrayList<>();
        for (int i = 0;i<100;i++){
            data.add("第"+i+"项");
        }
        return data;
    }

     static class RemoveItemListViewAdapter extends BaseAdapter {
        private ArrayList<String> data = new ArrayList<>();
        private LayoutInflater inflater;
        private Context context;

        public RemoveItemListViewAdapter(Context context) {
            this.inflater = LayoutInflater.from(context);
            this.context = context;
        }

        public void setData(ArrayList<String> data) {
            this.data = data;
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.item_remove_item_listview_layout, null);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            }else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.contentView.setText(data.get(position));
          /*  viewHolder.deleteView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    data.remove(position);
                    notifyDataSetChanged();
                }
            });*/
            convertView.scrollTo(0,0);
            return convertView;
        }

        static class ViewHolder {
            @Bind(R.id.item_remove_item_listview_content_textview)
            TextView contentView;
            @Bind(R.id.item_remove_item_listview_delete_textview)
            TextView deleteView;

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }
}
